package com.qiujie.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiujie.dto.Response;
import com.qiujie.dto.ResponseDTO;
import com.qiujie.dto.RegisterDto;
import com.qiujie.entity.Staff;
import com.qiujie.entity.StaffDetails;
import com.qiujie.entity.StaffRole;
import com.qiujie.mapper.StaffMapper;
import com.qiujie.mapper.StaffRoleMapper;
import com.qiujie.util.JwtUtil;
import com.qiujie.vo.StaffDeptVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

/**
 * @Author : qiujie
 * @Date : 2022/1/30
 */

@Service
public class LoginService extends ServiceImpl<StaffMapper, Staff> {

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private StaffRoleMapper staffRoleMapper;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private PasswordEncoder passwordEncoder;



    public ResponseDTO login(Staff staff) {
        // 校验验证码
//        String codeInRedis = redisUtil.get("validate:code").toString();
//        if(codeInRedis == null){
//            return Response.error("验证码不存在！");
//        }
//        LocalDateTime expireTime = LocalDateTime.parse(redisUtil.get("expire:time").toString());
//        if(ValidateCodeUtil.isExpire(expireTime)){
//            return Response.error("验证码过期！");
//        }
//        if (!codeInRedis.equals(validateCode)) {
//            return Response.error("验证码错误！");
//        }
        // AuthenticationManager authenticationManager进行用户认证
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
                new UsernamePasswordAuthenticationToken(staff.getCode(), staff.getPassword());
        Authentication authenticate = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
        // 认证失败框架会抛出异常
        // 认证通过
        StaffDetails staffDetails = (StaffDetails) authenticate.getPrincipal();
        String token = JwtUtil.generateToken(staffDetails);
        StaffDeptVO staffDeptVO = this.staffMapper.queryByCode(staffDetails.getUsername());

        return Response.success(staffDeptVO, token);
    }

    public ResponseDTO register(RegisterDto dto) {

        Staff staff = new Staff();
        StaffRole staffRole = new StaffRole();
        staff.setPassword(passwordEncoder.encode(dto.getPassword())).setCode(dto.getUserName());
        staff.setName(dto.getName());
        staff.setStatus(1);
        staff.setDeleteFlag(0);
        if(dto.getUserType()==2){
            staffRole.setRoleId(2);
            staff.setDeptId(3);
        }else if (dto.getUserType()==3){
            staffRole.setRoleId(3);
            staff.setDeptId(4);
        }
        staffMapper.insert(staff);

        staffRole.setStaffId(staff.getId());
        staffRoleMapper.insert(staffRole);

        return Response.success();
    }
}
